﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using GtsServer.Common;
using GtsServer.Model;

namespace GtsServer.DAL
{
    /// <summary>
    /// 数据访问类:ChanceMould
    /// </summary>
    public partial class ChanceMouldDAL
    {
        public ChanceMouldDAL()
        { }
        #region  Method



        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(GtsServer.Model.ChanceMould model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into ChanceMould(");
            strSql.Append("MouldName,SortNum,TypeName,IsTaskCondition)");
            strSql.Append(" values (");
            strSql.Append("@MouldName,@SortNum,@TypeName,@IsTaskCondition);select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@MouldName", SqlDbType.VarChar,50),
					new SqlParameter("@SortNum", SqlDbType.Int),
					new SqlParameter("@TypeName", SqlDbType.VarChar,50),
					new SqlParameter("@IsTaskCondition", SqlDbType.Bit,1)};
            parameters[0].Value = model.MouldName;
            parameters[1].Value = model.SortNum;
            parameters[2].Value = model.TypeName;
            parameters[3].Value = model.IsTaskCondition;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        public bool Update(ChanceMould model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update ChanceMould set ");
            strSql.Append("MouldName=@MouldName,SortNum=@SortNum,TypeName=@TypeName,IsTaskCondition=@IsTaskCondition");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
                    new SqlParameter("@MouldName", SqlDbType.VarChar,50),
                    new SqlParameter("@SortNum", SqlDbType.Int),
                    new SqlParameter("@TypeName", SqlDbType.VarChar,20),
					new SqlParameter("@IsTaskCondition", SqlDbType.Bit,1),
                    new SqlParameter("@id", SqlDbType.Int)};
            parameters[0].Value = model.MouldName;
            parameters[1].Value = model.SortNum;
            parameters[2].Value = model.TypeName;
            parameters[3].Value = model.IsTaskCondition;
            parameters[4].Value = model.ID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {
            //该表无主键信息，请自定义主键/条件字段
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from ChanceMould ");
            strSql.Append(" where id='" + id + "'");
            SqlParameter[] parameters = {
};

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public GtsServer.Model.ChanceMould GetModel()
        {
            //该表无主键信息，请自定义主键/条件字段
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 MouldName from ChanceMould ");
            strSql.Append(" where ");
            SqlParameter[] parameters = {
};

            GtsServer.Model.ChanceMould model = new GtsServer.Model.ChanceMould();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["MouldName"] != null && ds.Tables[0].Rows[0]["MouldName"].ToString() != "")
                {
                    model.MouldName = ds.Tables[0].Rows[0]["MouldName"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,MouldName,SortNum,TypeName,IsTaskCondition,case IsTaskCondition when '1' then '是' when '0' then '否' end IsTaskConditionName ");
            strSql.Append(" FROM ChanceMould ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" ID,MouldName,SortNum,TypeName,IsTaskCondition ");
            strSql.Append(" FROM ChanceMould ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "ChanceMould";
            parameters[1].Value = "ChanceTypeID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}

